var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: ['基础资料', '相册', 'Ta动态'],
    tabIndex: 0,
    bg_imgs: [
      'https://www.58zltc.com/Public/images/wx/quan_bg000.png',
      'https://www.58zltc.com/Public/images/wx/quan_bg001.png'
    ],
    is_anlian: 0,
    is_friend: 0,
    no_card:0
  },
  follow: function (e) {
    var that = this;
    wx.showLoading({
      title: '加载中…',
    })
    var data = {};
    data.openid = app.globalData.openid;
    data.follow_openid = e.currentTarget.dataset.openid;
    app.ihttpPost('https://www.58zltc.com/api.php/Quan/follow', data, function (res) {
      if (res.data.err_code === 0) {
        wx.showToast({
          title: "成功"
        })
        setTimeout(function () {
          wx.hideLoading()
          that.onLoad()
        }, 1200)

      } else {
        wx.showToast({
          title: res.data.err_msg
        })
      }
    })

  },
  setTab: function (e) {
    var index = e.currentTarget.dataset.index;
    this.setData({
      tabIndex: index
    })
  },
  chat:function(e){
    if(this.data.no_card == 1){
      wx.showModal({
        title: '提示',
        content: '还没有开通同城卡，是否开通？',
        success: function (res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/shop/tccar/tccar',
            })
          } else if (res.cancel) {
            wx.navigateBack({})
          }
        }
      })
    }else{
      var openid = e.currentTarget.dataset.openid;
      if (openid == app.globalData.openid) {
        wx.showToast({
          title: '不能和自己聊天！',
          icon: 'none'
        })
      } else {
        wx.navigateTo({
          url: 'chatroom?openid=' + openid,
        })
      }
    }
    
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    //随机背景
    let img_num = Math.round(Math.random());//0~1
    this.setData({
      bg_img: that.data.bg_imgs[img_num]
    })
    if (that.data.openid){
      var openid = that.data.openid
    }else{
      var openid = options.openid
      this.setData({
        openid: options.openid,
        my_openid: app.globalData.openid
      })
    }
    wx.showLoading({
      title: '加载中…',
    })
    //访问
    app.ihttpGet('https://www.58zltc.com/api.php/Quan/addVisit/openid/' + openid + '/myopenid/' + app.globalData.openid, function (res) {
      that.setData({
        visit_num: res.data
      })
    });
   
    app.ihttpGet('https://www.58zltc.com/api.php/Quan/member/openid/' + openid  + '/mopenid/'+app.globalData.openid, function (res) {
      that.setData({
        info: res.data
      })
      if (!res.data.me.user_label){
          wx.showModal({
            title: '提示',
            content: '请先完善信息',
            success:function(){
              wx.navigateTo({
                url: '/pages/mine/edit/edit',
              })
            }
          })
          return false;
      }else{
        that.setData({
          info: res.data
        })
      }
    });
    app.ihttpGet('https://www.58zltc.com/api.php/Quan/increaseVisitNum/openid/' + openid , function (res) {});
    //暗恋
    app.ihttpGet('https://www.58zltc.com/api.php/Quan/isAnlian/openid/' + openid + '/myopenid/' + app.globalData.openid, function (res) {
      that.setData({
        is_anlian: res.data
      })
    });
    //好友
    app.ihttpGet('https://www.58zltc.com/api.php/Quan/isFriend/openid/' + openid + '/myopenid/' + app.globalData.openid, function (res) {
      that.setData({
        is_friend: res.data
      })
    });
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
   

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var openid = this.data.openid
    var that =this
    if (openid != app.globalData.openid) {
      //同城卡
      app.ihttpGet('https://www.58zltc.com/api.php/Shop/haveTCCard/openid/' + app.globalData.openid, function (res) {
        if (res.data.have_car == 0) {
          that.setData({
            no_card: 1
          })
        }else{
          that.setData({
            no_card: 0
          })
        }
      });
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  beback: function (e) {
    wx.navigateBack({
      
    })
  },
  anLian: function (e) {
    let that = this;
    var an_openid = e.currentTarget.dataset.openid;
    var from_openid = app.globalData.openid;
    var that_is_anlian = that.data.is_anlian;
    var change_is_anlian = that_is_anlian ? 0 : 1;
    var change_title = change_is_anlian ? '暗恋成功' : '已取消';
    var change_icon = change_is_anlian ? 'success' : 'none';
    if (an_openid == from_openid) {
      change_title = change_is_anlian ? '爱自己亦爱生活 : )' : '';
      change_icon = 'none';
    }
    that.setData({
      is_anlian: change_is_anlian
    })
    app.ihttpGet('https://www.58zltc.com/api.php/Quan/changeAnlian/openid/' + an_openid + '/myopenid/' + app.globalData.openid + '/state/' + change_is_anlian, function (res) {
      if (!res.data) {
        wx.showToast({
          title: '网络繁忙~',
          icon: 'none',
          duration: 1200,
        })
      }
    })
    if (change_title) {
      wx.showToast({
        title: change_title,
        icon: change_icon,
        duration: 1500,
      })
    }
  },
  addFriend: function (e) {
    let that = this;
    var add_openid = e.currentTarget.dataset.openid;
    var from_openid = app.globalData.openid;
    var that_is_friend = that.data.is_friend;
    var change_is_friend = that_is_friend ? 0 : 1;
    var showTitle = change_is_friend ? '已添加好友' : '已取消';
    var showIcon = change_is_friend ? 'success' : 'none';
    that.setData({
      is_friend: change_is_friend
    })
    app.ihttpGet('https://www.58zltc.com/api.php/Quan/changeFriend/openid/' + add_openid + '/myopenid/' + app.globalData.openid + '/state/' + change_is_friend, function (res) {
      if (!res.data) {
        wx.showToast({
          title: '网络繁忙~',
          icon: 'none',
          duration: 1200,
        })
      }
    })
    wx.showToast({
      title: showTitle,
      icon: showIcon,
      duration: 1200,
    })
  }
})